package cn.example.demo.modules.house.controller;

import cn.example.demo.common.model.response.HttpDataResponseResult;
import cn.example.demo.common.model.response.HttpResponseResult;
import cn.example.demo.common.model.service.ServiceResult;
import cn.example.demo.common.retrieval.PageBean;
import cn.example.demo.common.secure.annotation.AuthEnable;
import cn.example.demo.common.validation.groups.*;
import cn.example.demo.modules.house.dto.HousingOrderDto;
import cn.example.demo.modules.house.entity.HousingOrder;
import cn.example.demo.modules.house.service.IHousingOrderService;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 【租房订单】控制器
 * </p>
 */
@Validated
@RestController
@RequestMapping("api/HousingOrder")
@Api(tags = {"【租房订单】操作接口"})
@Slf4j
public class HousingOrderController {
    @Resource
    private IHousingOrderService housingOrderService;

    @AuthEnable
    @PostMapping("add")
    @ApiOperation(value = "新增【租房订单】")
    public HttpResponseResult addHousingOrder(@ApiParam(value = "租房订单请求参数体") @RequestBody @Validated(Insert.class) HousingOrderDto dto) {
        // 保存HousingOrder
        ServiceResult result = housingOrderService.insertHousingOrder(dto);
        if (result.getStatus() == HttpStatus.OK.value()) {
            return HttpDataResponseResult.isSuccess(result.getMessage(), null);
        }
        return HttpDataResponseResult.isNotModified(result.getMessage(), result.getData());
    }

    @AuthEnable
    @PutMapping("modify")
    @ApiOperation(value = "修改【租房订单】")
    public HttpResponseResult modifyHousingOrder(@ApiParam(value = "租房订单请求参数体") @RequestBody @Validated(Update.class) HousingOrderDto dto) {
        // 保存HousingOrder
        ServiceResult result = housingOrderService.updateHousingOrder(dto);
        if (result.getStatus() == HttpStatus.OK.value()) {
            return HttpDataResponseResult.isSuccess(result.getMessage(), null);
        }

        return HttpDataResponseResult.isNotModified(result.getMessage(), result.getData());
    }

    @AuthEnable
    @GetMapping(value = "retrieval")
    @ApiOperation(value = "查询【租房订单】")
    public HttpResponseResult getHousingOrderList(@ApiParam(value = "租房订单请求参数体") HousingOrderDto dto) {
        PageBean result = housingOrderService.queryHousingOrder(dto);

        if (result.getItems() == null || result.getItems().isEmpty()) {
            return HttpDataResponseResult.isNotFound("未查询到【租房订单】记录！", null);
        }

        return HttpDataResponseResult.isSuccess(result);
    }

    @AuthEnable
    @GetMapping(value = "retrieval/owner")
    @ApiOperation(value = "查询个人【租房订单】")
    public HttpResponseResult getOwnedHousingOrderList(@ApiParam(value = "租房订单请求参数体") HousingOrderDto dto) {
        PageBean result = housingOrderService.queryOwnedHousingOrder(dto);

        if (result.getItems() == null || result.getItems().isEmpty()) {
            return HttpDataResponseResult.isNotFound("未查询到【租房订单】记录！", null);
        }

        return HttpDataResponseResult.isSuccess(result);
    }

    @AuthEnable
    @GetMapping(value = "find/{id}")
    @ApiOperation(value = "根据 ID 查询单个【租房订单】")
    public HttpResponseResult getOneHousingOrder(@ApiParam(value = "租房订单ID") @PathVariable @NotBlank(message = "id不能为空！") String id) {
        HousingOrder result = housingOrderService.findHousingOrderById(id);

        if (result == null) {
            return HttpDataResponseResult.isNotFound(StrUtil.format("未查询到【租房订单】记录！ID:{}", id), null);
        }

        return HttpDataResponseResult.isSuccess(result);
    }

    @AuthEnable
    @DeleteMapping("remove")
    @ApiOperation(value = "删除【租房订单】")
    public HttpResponseResult removeHousingOrder(@ApiParam(value = "租房订单请求参数体") @RequestBody @Validated(Delete.class) HousingOrderDto dto) {
        ServiceResult result = housingOrderService.deleteHousingOrder(dto);

        if (result.getStatus() == HttpStatus.OK.value()) {
            return HttpDataResponseResult.isSuccess(result.getMessage(), result.getData());
        }

        return HttpDataResponseResult.isNotModified(result.getMessage(), result.getData());
    }

    @AuthEnable
    @GetMapping(value = "dictList")
    @ApiOperation(value = "以字典表格式查询【租房订单】")
    public HttpResponseResult getHousingOrderAsDict(@ApiParam(value = "租房订单请求参数体") HousingOrderDto dto) {
        List result = housingOrderService.queryHousingOrderAsDict(dto);
        if (ObjectUtil.isEmpty(result)) {
            return HttpDataResponseResult.isNotFound("【租房订单】列表：没有数据！", null);
        }

        return HttpDataResponseResult.isSuccess(result);
    }

    @AuthEnable
    @ApiOperation(value = "订单状态（批量操作）")
    @PutMapping(value = "orderStatus/modify/batch")
    public HttpResponseResult orderStatusBatchModify(@RequestBody @Validated({Batch.class}) HousingOrderDto dto) {
        ServiceResult result = housingOrderService.batchUpdateStatus(dto);

        if (result.getStatus() == HttpStatus.OK.value()) {
            return HttpDataResponseResult.isSuccess(result.getMessage(), result.getData());
        }

        return HttpDataResponseResult.isNotModified(result.getMessage(), result.getData());
    }

    @AuthEnable
    @PutMapping("pay")
    @ApiOperation(value = "支付【租房订单】")
    public HttpResponseResult payHousingOrder(@ApiParam(value = "租房订单请求参数体") @RequestBody(required = true) @Validated(Identity.class) HousingOrderDto dto) {
        // 保存HousingOrder
        dto.setOrderStatus(2);
        dto.setPrePayDate(new Date());
        ServiceResult result = housingOrderService.updateHousingOrder(dto);
        if (result.getStatus() == HttpStatus.OK.value()) {
            try {
                // 邮件通知
                housingOrderService.mailAdvice(dto.getId());
            } catch (Exception e) {
                log.error(e.getMessage(), e);
            }
            return HttpDataResponseResult.isSuccess(result.getMessage(), null);
        }
        return HttpDataResponseResult.isNotModified(result.getMessage(), result.getData());
    }

    /**
     * TODO-lzx 临时功能：完成评价与回复
     *
     * @param dto
     * @return
     */
    @AuthEnable
    @PostMapping("modify/base")
    @ApiOperation(value = "修改【租房订单】基本信息（评价，回复）")
    public HttpResponseResult modifyBaseHousingOrderInfo(@ApiParam(value = "租房订单请求参数体") @RequestBody @Validated(Identity.class) HousingOrderDto dto) {
        // 保存HousingOrder
        ServiceResult result = housingOrderService.updateHousingOrder(dto);
        if (result.getStatus() == HttpStatus.OK.value()) {
            return HttpDataResponseResult.isSuccess(result.getMessage(), null);
        }
        return HttpDataResponseResult.isNotModified(result.getMessage(), result.getData());
    }

    @GetMapping(value = "retrieval/appraise/list")
    @ApiOperation(value = "查询【租房客户评价】列表")
    public HttpResponseResult getHousingOrderAppraiseList(@ApiParam(value = "租房订单请求参数体") HousingOrderDto dto) {
        PageBean result = housingOrderService.queryHousingOrderWithUserInfo(dto);

        if (result.getItems() == null || result.getItems().isEmpty()) {
            return HttpDataResponseResult.isNotFound("未查询到【租房客户评价】记录！", null);
        }

        return HttpDataResponseResult.isSuccess(result);
    }

    @AuthEnable
    @GetMapping(value = "data/statistic")
    @ApiOperation(value = "数据统计")
    public HttpResponseResult getActivityStatistic() {
        ServiceResult result = housingOrderService.dataStatistic();

        if (result.getStatus() == HttpStatus.OK.value()) {
            return HttpDataResponseResult.isSuccess(result.getMessage(), result.getData());
        }

        return HttpDataResponseResult.isNotFound(result.getMessage(), null);
    }

    @AuthEnable
    @PutMapping("continue")
    @ApiOperation(value = "续房")
    public HttpResponseResult continueHousingOrder(@ApiParam(value = "租房订单请求参数体") @RequestBody @Validated(Identity.class) HousingOrderDto dto) {
        // 保存HousingOrder
        ServiceResult result = housingOrderService.continueHousing(dto);
        if (result.getStatus() == HttpStatus.OK.value()) {
            return HttpDataResponseResult.isSuccess(result.getMessage(), null);
        }

        return HttpDataResponseResult.isNotModified(result.getMessage(), result.getData());
    }

    @AuthEnable
    @PutMapping("finish")
    @ApiOperation(value = "退房")
    public HttpResponseResult finishHousingOrder(@ApiParam(value = "租房订单请求参数体") @RequestBody @Validated(Identity.class) HousingOrderDto dto) {
        // 保存HousingOrder
        ServiceResult result = housingOrderService.finishHousing(dto);
        if (result.getStatus() == HttpStatus.OK.value()) {
            return HttpDataResponseResult.isSuccess(result.getMessage(), null);
        }

        return HttpDataResponseResult.isNotModified(result.getMessage(), result.getData());
    }
}
